package com.chinawlx.wlxteacher.widget.calendar;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.chinawlx.wlxteacher.utils.WLXLogUtil;

/**
 * 作者:吴名
 * 时间:16/9/10
 * 描述:根据child数量以及高度自适应的ViewPager(系统的ViewPager会占全屏)
 */
public class WrapContentHeightViewPager extends ViewPager {


    public WrapContentHeightViewPager(Context context) {
        super(context);
    }

    public WrapContentHeightViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int height = 0;
        //下面遍历所有child的高度
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            child.measure(widthMeasureSpec, View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec
                    .UNSPECIFIED));
            int h = child.getMeasuredHeight();
            if (h > height) //采用最大的view的高度。
                height = h;
        }

        heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    private int startX;
    private int startY;
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startX = (int) ev.getX();
                startY = (int) ev.getY();
                WLXLogUtil.printE(this.getCurrentItem() + "");
                if (this.getCurrentItem() == 0) {
                    getParent().requestDisallowInterceptTouchEvent(false);
                } else {
                    getParent().requestDisallowInterceptTouchEvent(true);
                }
                break;
            case MotionEvent.ACTION_MOVE:
                int dX = (int) (ev.getX() - startX);
                int dY = (int) (ev.getY() - startY);
                if (Math.abs(dX) > Math.abs(dY)) {//左右滑动
//                    return true;
//                    getParent().requestDisallowInterceptTouchEvent(true);
                } else {//上下滑动
                    getParent().requestDisallowInterceptTouchEvent(false);
                    return false;
                }
                if (this.getCurrentItem() == 0) {
                    getParent().requestDisallowInterceptTouchEvent(false);
                } else {
                    getParent().requestDisallowInterceptTouchEvent(true);
                }
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

//    private int startX;
//    private int startY;
//
//    @Override
//    public boolean onInterceptTouchEvent(MotionEvent ev) {
//        switch (ev.getAction()) {
//            case MotionEvent.ACTION_DOWN:
//                startX = (int) ev.getX();
//                startY = (int) ev.getY();
//                break;
//            case MotionEvent.ACTION_MOVE:
//
//                int dX = (int) (ev.getX() - startX);
//                int dY = (int) (ev.getY() - startX);
//                if (Math.abs(dX) > Math.abs(dY)&&this.getCurrentItem() != 0) {//左右滑动
//                    return true;
//                    getParent().requestDisallowInterceptTouchEvent(true);
//                } else {//上下滑动
//                    return false;
//                    getParent().requestDisallowInterceptTouchEvent(false);
//                }
//            case MotionEvent.ACTION_UP:
//                break;
//        }
//        return super.onInterceptTouchEvent(ev);
//    }
}
